<?php
/**
 * Knihovna_Acl
 *
 * @author Petr Laslo
 * @license MIT License
 * @package Knihovna
 * @link https://code.google.com/p/knihovna/
 */
 
class Knihovna_Acl extends Zend_Acl {

    /*
     * @param void
     * @return void
     */
    public function __construct()
    {
        $roles = new Knihovna_Model_DbTable_Usersrole();
        foreach($roles->fetchAll(null,"id") as $key=>$val)
        {
            if(!empty($val->rodic))
            {
                $this->addRole(new Zend_Acl_Role($val->nazev),$val->rodic);
            }else{
                $this->addRole(new Zend_Acl_Role($val->nazev));
            }
        }

        $resources = new Knihovna_Model_DbTable_Usersresources();
        foreach($resources->fetchAll(null) as $key=>$val)
        {
            if(!empty($val->rodic))
            {
                $this->addResource(new Zend_Acl_Resource($val->nazev),$val->rodic);
            }else{
                $this->addResource(new Zend_Acl_Resource($val->nazev));
            }
        }

        /*$this->addResource(new Zend_Acl_Resource('index'));
        $this->addResource(new Zend_Acl_Resource('error'));
        $this->addResource(new Zend_Acl_Resource('user'));
        $this->addResource(new Zend_Acl_Resource('autor'));
        $this->addResource(new Zend_Acl_Resource('lokace'));

        $this->addResource(new Zend_Acl_Resource('admin'));
        $this->addResource(new Zend_Acl_Resource('admin:index'),'admin');
        $this->addResource(new Zend_Acl_Resource('admin:users'),'admin');
        $this->addResource(new Zend_Acl_Resource('admin:books'),'admin');
        $this->addResource(new Zend_Acl_Resource('admin:vydavatel','admin'));
        $this->addResource(new Zend_Acl_Resource('admin:tskupina','admin'));
        $this->addResource(new Zend_Acl_Resource('admin:titul','admin'));
        $this->addResource(new Zend_Acl_Resource('admin:search','admin'));
        $this->addResource(new Zend_Acl_Resource('admin:autor','admin'));
        $this->addResource(new Zend_Acl_Resource('admin:lokace','admin'));
        $this->addResource(new Zend_Acl_Resource('admin:role','admin'));
        $this->addResource(new Zend_Acl_Resource('admin:resources','admin'));
        $this->addResource(new Zend_Acl_Resource('admin:privileges','admin'));*/

        //$this->deny();

        $privileges = new Knihovna_Model_DbTable_Usersprivileges();
        foreach($privileges->fetchAll() as $key=>$val)
        {
            if($val->resource == "%")
            {
                foreach($resources->fetchAll(null) as $key=>$val2)
                {
                    $this->allow($val->role,$val2->nazev);
                }
            }elseif($val->resource == "-")
            {
                foreach($resources->fetchAll(null) as $key=>$val2)
                {
                    $this->deny($val->role,$val2->nazev);
                }
            }elseif($val->privilege == "%")
            {
                $this->allow($val->role,$val->resource);
            }elseif($val->privilege == "-")
            {
                $this->deny($val->role,$val->resource);
            }else{
                $priv = explode(",",$val->privilege);
                if($priv[0] == "-"){
                    $this->deny($val->role,$val->resource,$priv);
                }else{
                    $this->allow($val->role,$val->resource,$priv);
                }
            }
        }
        //$this->allow('admin');

        /*$this->allow('guest','index');
        $this->allow('guest','error');
        $this->allow('guest','user',array('login','forgot'));

        $this->allow('user','index');
        $this->allow('user','error');
        $this->allow('user','user',array('logout'));

        $this->deny('admin','user',array('login'));*/
    }

    /*
    public function __construct($acl)
    {
        foreach ($acl['roles'] as $role) {
            $this->addRole(new Zend_Acl_Role($role));
        }

        foreach ($acl['ressources'] as $resource) {
            $this->addResource(new Zend_Acl_Resource($resource));
        }

        foreach ($acl['rules'] as $function => $rule) {
            foreach ($rule as $role => $rule2) {
                foreach ($rule2 as $resource => $rule3) {
                    if ('all' == $rule3) {
                        $this->$function($role, $resource);
                    } else {
                        $this->$function($role, $resource, $rule3);
                    }
                }
            }
        }
    }*/
}
